#include "aerial\Game\GameObject\Equipment\Shield.h"

#include "aerial\Game\GameObject\FlyObject\FlyObject.h"

#include <iostream>

using namespace std;

const float Shield::MinPower = 5.f;
const float Shield::PCPS = 2.f;

bool Shield::available(FlyObject* fObj)
{
	return fObj->GetPower() > MinPower;
}

Shield::Shield(float _radius, FlyObject* _host): max_shield_radius(_radius), host(_host)
{
	sprShield = hgeResMan->GetSprite("spr.shield");
	shield_scale = 0.f;
	halo_scale = 0.f;
	shieldState = Off;
}

bool Shield::isOn()
{
	return (shieldState == Expanding || shieldState == Full);
}

bool Shield::ShieldOn()
{
	if (host->GetPower() >= MinPower) 
	{
		if (shieldState != Full) shieldState = Expanding;
		return true;
	}
	else {
		cout << "not enough power.\n";
		return false;
	}
}

void Shield::ShieldOff()
{
	if (shieldState != Off) shieldState = Contracting;
}

bool Shield::update(float dt)
{
	if (shieldState != Off)
	{
		if (shieldState == Expanding)
		{
			shield_scale += 5.f * dt;
			if (shield_scale > max_shield_radius)
			{
				shield_scale = max_shield_radius;
				shieldState = Full;
			}
		}
		else if (shieldState == Contracting)
		{
			shield_scale -= 5.f * dt;
			if (shield_scale < 0.f)
			{
				shield_scale = 0.f;
				shieldState = Off;
			}
		}
		host->power -= shield_scale * dt * PCPS;
		if (host->power < 0) 
		{
			host->power = 0;
			shieldState = Off;
			shield_scale = 0.f;
			return true;
		} else
		{
			halo_scale += 2.5f * dt;
			if (halo_scale > shield_scale) halo_scale = 0.f;
		}
	}
	return false;
}

void Shield::renderShield(float x, float y)
{
	if (shieldState != Off)
	{
		sprShield->SetColor(0xff0000ff);
		sprShield->RenderEx( x, y, 0.f, shield_scale, shield_scale);
		sprShield->SetColor(0xffffffff);
		sprShield->RenderEx( x, y, 0.f, halo_scale, halo_scale);
	}
}



